/*     */ package com.zimbra.cs.rmgmt;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintStream;
/*     */ import java.io.Reader;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RemoteResultParser
/*     */ {
/*  43 */   private static final Pattern KEY_VALUE_PATTERN = Pattern.compile("^([^=]+)=(.*)$");
/*     */   private static final int KEY_GROUP = 1;
/*     */   private static final int VALUE_GROUP = 2;
/*     */   
/*     */   public static void parse(Reader reader, Visitor visitor) throws IOException {
/*  48 */     BufferedReader in = new BufferedReader(reader);
/*     */     
/*  50 */     int lineNumber = 0;
/*     */     
/*  52 */     Map<String, String> current = new HashMap();
/*  53 */     int currentMapStartLineNumber = 1;
/*     */     String line;
/*  55 */     while ((line = in.readLine()) != null) {
/*  56 */       lineNumber++;
/*  57 */       Matcher matcher = KEY_VALUE_PATTERN.matcher(line);
/*  58 */       if (ZimbraLog.rmgmt.isDebugEnabled()) ZimbraLog.rmgmt.debug("Scanning mail queues. Read line: " + line);
/*  59 */       if (!matcher.find()) {
/*  60 */         if (ZimbraLog.rmgmt.isDebugEnabled()) ZimbraLog.rmgmt.debug("Scanning mail queues. Matcher did not find any mathces.");
/*  61 */         String id = (String)current.get(RemoteMailQueue.QueueAttr.id.toString());
/*  62 */         if (id != null)
/*     */         {
/*     */ 
/*  65 */           visitor.handle(currentMapStartLineNumber, current);
/*  66 */           current = new HashMap();
/*  67 */           currentMapStartLineNumber = lineNumber + 1;
/*     */         }
/*  69 */       } else { current.put(matcher.group(1), matcher.group(2));
/*     */       }
/*     */     }
/*  72 */     if (!current.isEmpty()) {
/*  73 */       visitor.handle(currentMapStartLineNumber, current);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void parse(InputStream is, Visitor v) throws IOException {
/*  78 */     parse(new InputStreamReader(is), v);
/*     */   }
/*     */   
/*     */   public static void parse(RemoteResult rr, Visitor v) throws IOException {
/*  82 */     parse(new InputStreamReader(new ByteArrayInputStream(rr.mStdout)), v);
/*     */   }
/*     */   
/*     */   private static class SingleVisitor implements RemoteResultParser.Visitor {
/*     */     private Map<String, String> mValue;
/*     */     
/*     */     public void handle(int lineNo, Map<String, String> map) {
/*  89 */       this.mValue = map;
/*     */     }
/*     */   }
/*     */   
/*     */   public static Map<String, String> parseSingleMap(Reader reader) throws IOException {
/*  94 */     SingleVisitor v = new SingleVisitor(null);
/*  95 */     parse(reader, v);
/*  96 */     return v.mValue;
/*     */   }
/*     */   
/*     */ 
/* 100 */   public static Map<String, String> parseSingleMap(InputStream is) throws IOException { return parseSingleMap(new InputStreamReader(is)); }
/*     */   
/*     */   public static void main(String[] args) throws IOException {
/*     */     InputStream is;
/*     */     InputStream is;
/* 105 */     if (args.length == 0) {
/* 106 */       is = System.in;
/*     */     } else {
/* 108 */       is = new FileInputStream(args[0]);
/*     */     }
/*     */     
/* 111 */     parse(new InputStreamReader(is), new Visitor()
/*     */     {
/*     */       public void handle(int lineNumber, Map<String, String> map) {
/* 114 */         for (String key : map.keySet()) {
/* 115 */           System.out.print(key);
/* 116 */           System.out.print("=");
/* 117 */           System.out.println((String)map.get(key));
/*     */         }
/* 119 */         System.out.println();
/*     */       }
/*     */     });
/*     */   }
/*     */   
/*     */   public static Map<String, String> parseSingleMap(RemoteResult rr) throws IOException {
/* 125 */     return parseSingleMap(new InputStreamReader(new ByteArrayInputStream(rr.mStdout)));
/*     */   }
/*     */   
/*     */   public static abstract interface Visitor
/*     */   {
/*     */     public abstract void handle(int paramInt, Map<String, String> paramMap)
/*     */       throws IOException;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/rmgmt/RemoteResultParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */